package com.myk.game.gobangchess.ai.startzone;

public class Get_Fitness {

    public static int get_fitness(int[][] input, int targetColor) {
        int[][] inputLarger = new int[11][11];
        // Fill the larger input with 3s
        for (int i = 0; i < inputLarger.length; i++) {
            for (int j = 0; j < inputLarger[i].length; j++) {
                inputLarger[i][j] = 3;
            }
        }
        // Place the original input in the center
        for (int i = 1; i <= input.length; i++) {
            for (int j = 1; j <= input[i - 1].length; j++) {
                inputLarger[i][j] = input[i - 1][j - 1];
            }
        }

        int[] typesFitting = new int[16];
        for (int i = 1; i <= 9; i++) { // Adjusted loop bounds for 0-indexing
            for (int j = 1; j <= 9; j++) {
                if (inputLarger[i][j] == targetColor) {
                    int[] directionsFitting = {0, 0, 0, 0};
                    for (int k = 0; k < 4; k++) { // Adjusted for 0-indexing
                        int row = i, col = j, ends = 0;
                        if (k == 0) { // Down
                            row = i + 1;
                        } else if (k == 1) { // Up
                            row = i - 1;
                        } else if (k == 2) { // Right
                            col = j + 1;
                        } else if (k == 3) { // Left
                            col = j - 1;
                        }

                        while (inputLarger[row][col] != 3 && ends == 0) {
                            if (inputLarger[row][col] == targetColor) {
                                ends = 1;
                                directionsFitting[k] = 1;
                            } else {
                                if (k == 0) row += 1;
                                else if (k == 1) row -= 1;
                                else if (k == 2) col += 1;
                                else if (k == 3) col -= 1;
                            }
                        }
                    }
                    int thisCaseNumIdx = directionsFitting[0] + 2 * directionsFitting[1] + 4 * directionsFitting[2] + 8 * directionsFitting[3];
                    typesFitting[thisCaseNumIdx] = 1;
                }
            }
        }

        int finalFitness = 0;
        for (int fit : typesFitting) {
            finalFitness += fit;
        }

        return finalFitness;
    }

    public static void main(String[] args) {

    }
}
